package com.itheima.a23;

import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.ServletRequestParameterPropertyValues;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ServletRequestDataBinderFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class CajTest {
    public static void main(String[] args) throws Exception {
        // 利用反射原理, 为 bean 的属性赋值
        TestBeanWrapper.MyBean target = new TestBeanWrapper.MyBean();
        BeanWrapperImpl wrapper = new BeanWrapperImpl(target);
        wrapper.setPropertyValue("a", "10");
        wrapper.setPropertyValue("b", "hello");
        wrapper.setPropertyValue("c", "1999/03/04");


        wrapper.setPropertyValue(new PropertyValue("c","2022/02/22"));
        System.out.println("-------setPropertyValue------");
        System.out.println(target);



        Map<String, String> map = new HashMap<>();
        map.put("a", "100");
        map.put("b", "下午好");
        map.put("c", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
        wrapper.setPropertyValues(map);
        System.out.println("------- wrapper.setPropertyValues(map)------");
        System.out.println(target);


        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.add("a", "10");
        pvs.add("b", "hello");
//        pvs.add("c", "1999/03/04");

        wrapper.setPropertyValues(pvs);

        System.out.println("------- wrapper.setPropertyValues(pvs)------");
        System.out.println(target);



        TestDataBinder.MyBean  target1 = new TestDataBinder.MyBean();
        DataBinder dataBinder = new DataBinder(target1);
        dataBinder.initDirectFieldAccess();
        pvs.add("c", "1999|03|04");
        dataBinder.addCustomFormatter(new MyDateFormatter("yyyy|MM|dd"),"c");
        dataBinder.bind(pvs);
        System.out.println("------- dataBinder.bind(pvs)------");
        System.out.println("target1 " + target1);


        System.out.println("============DirectFieldAccessor==========");
        TestBeanWrapper.MyBean target2 = new TestBeanWrapper.MyBean();
        DirectFieldAccessor accessor = new DirectFieldAccessor(target );
        accessor.setPropertyValue("a", "43243");
        accessor.setPropertyValue("b", "fsdf sdf");
        accessor.setPropertyValue("c", "1999/03/04");
         accessor.setWrappedInstance(target1);

        System.out.println("target1 " + target1);


        /**
         AbstractNestablePropertyAccessor 有2个子类
         1 BeanWrapperImpl
         2 DirectFieldAccessor
         */

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setParameter("birthday", "1999|01|02");
        request.setParameter("address.name", "西安");

        TestServletDataBinderFactory.User user = new TestServletDataBinderFactory.User();

        ServletRequestDataBinderFactory factory = new ServletRequestDataBinderFactory(null, null);

        WebDataBinder webDataBinder = factory.createBinder(new ServletWebRequest(request), user, "user");
        webDataBinder.addCustomFormatter(new MyDateFormatter(""));
        webDataBinder.bind(new ServletRequestParameterPropertyValues(request));
        System.out.println("user" + user);


    }
}
